{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Shor with Aqua"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Executing Shor's algorithm with Aqua is very simple. We just need to specify the number $N$ we want to factor and the value $a$ that we are going to use. \n",
    "\n",
    "We will start with the default values: $N=15$ and $a=2$. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "from qiskit import *\n",
    "from qiskit.visualization import *\n",
    "from qiskit.tools.monitor import *\n",
    "from qiskit.aqua import *\n",
    "from qiskit.aqua.algorithms import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shor = Shor(N=15, a = 2)\n",
    "backend = Aer.get_backend('qasm_simulator')\n",
    "quantum_instance = QuantumInstance(backend)\n",
    "result = shor.run(quantum_instance)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we are going to try $N=15$ and different values of $a$. We will first check that $N$ and $a$ do not share factors (that would be the trivial case!)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import gcd\n",
    "\n",
    "N=15\n",
    "print(\"N=\",N)\n",
    "for a in range(2,N):\n",
    "    print()\n",
    "    print(\"***** a=\",a)\n",
    "    print()\n",
    "    f = gcd(N,a)\n",
    "    if f==1:\n",
    "        shor = Shor(N,a)\n",
    "        result = shor.run(quantum_instance)\n",
    "        print(result[\"factors\"])\n",
    "    else:\n",
    "        print(\"A factor of\",N,\"is\",f)\n",
    "        "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
